/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.publications_analysis;

import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 *
 * @author yura
 */
public class DocFileProcessor extends FileProcessor {

//    /** \r\n - because Windows */
    public static final String LINE_END = "\n";//"\r\n";
    public static final String DOC = "doc";

    @Override
    protected String getName() {
        return DOC;
    }

    @Override
    protected String getTextDataFromFile(InputStream file, StringBuffer doc,
            int lineNum) throws IOException {
        StringBuilder sb = new StringBuilder();
        WordExtractor we = new WordExtractor(new HWPFDocument(new POIFSFileSystem(file)));
        for (String str : we.getParagraphText()) {
            sb.append(str).append(LINE_END);
            if (lineNum -- > 0) {
                doc.append(str).append(LINE_END);
            }
        }
        return sb.toString();
    }

}
